package com.test18_链表;

import org.junit.Test;

interface IBook {
    /** 获取图书名称 */
    public String getTitle();
    /** 获取图书价格 */
    public double getPrice();
    /** 获取图书作者 */
    public String getAuthor();
}

/** 数学书 */
class MathBook implements IBook {
    private String title;
    private double price;
    private String author;

    public MathBook() {
    }

    public MathBook(String title, double price, String author) {
        this.title = title;
        this.price = price;
        this.author = author;
    }

    @Override
    public String getTitle() {
        return this.title;
    }

    @Override
    public double getPrice() {
        return this.price;
    }

    @Override
    public String getAuthor() {
        return this.author;
    }

    @Override
    public String toString() {
        return "【数学】图书名称：" + this.title + "、图书价格：" + this.price + "、图书作者" + this.author;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (obj instanceof MathBook) {
            MathBook book = (MathBook) obj;
            return this.title.equals(book.title) && this.price == book.price && this.author.equals(book.author);
        }
        return false;
    }
}

/** 程序开发书 */
class ProgramBook implements IBook {
    private String title;
    private double price;
    private String author;

    public ProgramBook() {
    }

    public ProgramBook(String title, double price, String author) {
        this.title = title;
        this.price = price;
        this.author = author;
    }

    @Override
    public String getTitle() {
        return this.title;
    }

    @Override
    public double getPrice() {
        return this.price;
    }

    @Override
    public String getAuthor() {
        return this.author;
    }

    @Override
    public String toString() {
        return "【程序开发】图书名称：" + this.title + "、图书价格：" + this.price + "、图书作者" + this.author;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (obj instanceof ProgramBook) {
            ProgramBook book = (ProgramBook) obj;
            return this.title.equals(book.title) && this.price == book.price && this.author.equals(book.author);
        }
        return false;
    }
}

/** 大数据书 */
class BigDateBook implements IBook {
    private String title;
    private double price;
    private String author;

    public BigDateBook() {
    }

    public BigDateBook(String title, double price, String author) {
        this.title = title;
        this.price = price;
        this.author = author;
    }

    @Override
    public String getTitle() {
        return this.title;
    }

    @Override
    public double getPrice() {
        return this.price;
    }

    @Override
    public String getAuthor() {
        return this.author;
    }

    @Override
    public String toString() {
        return "【大数据】图书名称：" + this.title + "、图书价格：" + this.price + "、图书作者" + this.author;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (obj instanceof BigDateBook) {
            BigDateBook book = (BigDateBook) obj;
            return this.title.equals(book.title) && this.price == book.price && this.author.equals(book.author);
        }
        return false;
    }
}

/** 学生类 */
class Student {
    private ILink<IBook> books = new LinkImpl<>();  // 图书信息保存链表

    /** 图书购买 */
    public void buy(IBook book) {
        this.books.add(book);
    }

    /** 图书赠送 */
    public void give(IBook book) {
        this.books.remove(book);
    }

    /** 检索 */
    public ILink search(String keyword) {
        ILink<IBook> result = new LinkImpl<>();
        Object[] obj = this.books.toArray();
        for (Object temp : obj) {
            IBook book = (IBook) temp;
            if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
                result.add(book);
            }
        }
        return result;
    }

    public ILink<IBook> getBooks() {
        return this.books;
    }
}

/**
 * 链表应用
 */
public class YootkDemo_02 {

    @Test
    public void test1() {
        Student student = new Student();
        student.buy(new MathBook("布尔传奇", 67.8, "乔治·布尔"));
        student.buy(new MathBook("线性代数", 37.8, "国内数学家"));
        student.buy(new ProgramBook("Java从入门到项目实战", 99.8, "李兴华"));
        student.buy(new ProgramBook("Python从入门到项目实战", 99.8, "李兴华"));
        student.buy(new ProgramBook("Go从入门到项目实战", 99.8, "李兴华"));
        student.buy(new BigDateBook("Flink实时分析", 129.8, "小李老师"));
        student.buy(new BigDateBook("Spark实时分析", 159.8, "小李老师"));
        student.give(new ProgramBook("Java从入门到项目实战", 99.8, "李兴华"));     // 转送
        Object[] books = student.search("李").toArray();
        for (Object book : books) {
            System.out.println(book);
        }
    }
}
